unit CallByName_00Main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm2 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure CallMeByName(Sender: TObject) ;
  private
     procedure ExecMethod(OnObject: TObject; MethodName: string) ;
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}

procedure TForm2.Button1Click(Sender: TObject);
begin
ExecMethod(Form2, 'CallMeByName') ;
end;

procedure TForm2.CallMeByName(Sender: TObject) ;
begin
   ShowMessage('Hello Delphi!') ;
end;

type
   TExec = procedure of object;

procedure TForm2.ExecMethod(OnObject: TObject; MethodName: string) ;
var
   Routine: TMethod;
   Exec: TExec;
begin
   Routine.Data := Pointer(OnObject) ;
   Routine.Code := OnObject.MethodAddress(MethodName) ;
   if NOT Assigned(Routine.Code) then Exit;
   Exec := TExec(Routine) ;
   Exec;
end;

end.
